import * as Sequelize from "sequelize";
import { Application } from "egg";

interface RoleAttributes {
  id: string;
  role_name: string;
  status: string;
  type: string;
  remark: string;
  create_user_id: string;
  modify_user_id: string;
  create_time: string;
  modify_time: string;
}

type RoleInstance = Sequelize.Instance<RoleAttributes> & RoleAttributes;

export default (app: Application) => {
  const { STRING, CHAR, UUID, UUIDV4, DATE, NOW } = app.Sequelize;
  const attributes: SequelizeAttributes<RoleAttributes> = {
    id: {
      type: UUID,
      primaryKey: true,
      allowNull: false,
      defaultValue: UUIDV4
    },
    role_name: { type: STRING(50), allowNull: false },
    status: { type: CHAR(3), allowNull: false, defaultValue: "000" },
    type: { type: CHAR(3), allowNull: false, defaultValue: "000" },
    remark: { type: STRING(200), allowNull: false },
    create_user_id: { type: UUID, allowNull: false },
    modify_user_id: { type: UUID, allowNull: false },
    create_time: { type: DATE, allowNull: false, defaultValue: NOW },
    modify_time: { type: DATE, allowNull: false, defaultValue: NOW }
  };
  const model = app.model.define<RoleInstance, RoleAttributes>(
    "Role",
    attributes,
    {
      tableName: "t_role"
    }
  );
  model.associate = () => {
    app.model.models.Role.hasMany(app.model.models.RoleFunc, {
      foreignKey: "role_id"
    });
    app.model.models.Role.hasMany(app.model.models.RoleUser, {
      foreignKey: "role_id"
    });
  };
  return model;
};
